﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;

namespace WindowsFormsApplication1
{
    public partial class viewPreviousReports : Form
    {
        Pay pay;
        selection parent;
        MiscChargeController miscCharge;

        public viewPreviousReports(selection parent)
        {
            this.parent = parent;
            miscCharge = new MiscChargeController();
            InitializeComponent();
            initializeContent();
            viewReportsEmployee.SelectedIndex = -1;
        }

        private void back_Click(object sender, EventArgs e)
        {
            parent.Show();
            this.Hide();
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            DialogResult result = MessageBox.Show("Are you sure you want to exit the program?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.No)
                e.Cancel = true;
            else
                Environment.Exit(1);
        }

        public void initializeContent()
        {
            setEmptyDetails();
            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = miscCharge.getPreviousData();
            viewReportsPeriodTable.DataSource = bindingSource;
            DataTable employeeListSource = new DataTable();
            employeeListSource = new EmployeeController().getNames();
            viewReportsEmployee.Items.Clear();
            for (int i = 0; i < employeeListSource.Rows.Count; i++)
                viewReportsEmployee.Items.Add((employeeListSource.Rows[i][0].ToString() + ", " + employeeListSource.Rows[i][1].ToString()));
        }

        private void setEmptyDetails()
        {
            nameLabel.Text = "";
            salaryLabel.Text = 0+"";
            tipLabel.Text = 0 + "";
            chargesLabel.Text = 0 + "";
            lateLabel.Text = 0 + "";
            absentLabel.Text = 0 + "";
            debtsLabel.Text = 0 + "";
            bonusesLabel.Text = 0 + "";
            deductionsLabel.Text = 0 + "";
            sssLabel.Text = 0 + "";
            philHealthLabel.Text = 0 + "";
            payLabel.Text = 0 + "";
        }

        private void viewReportsEmployee_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (viewReportsEmployee.SelectedIndex > -1)
                setReportDetails();
        }

        private void setReportDetails()
        {
            EmployeeController empControl = new EmployeeController();
            String selectedFname = viewReportsEmployee.SelectedItem.ToString().Split(',')[1].Trim();
            String selectedLname = viewReportsEmployee.SelectedItem.ToString().Split(',')[0];
            String startDate = viewReportsPeriodTable.SelectedRows[0].Cells[0].Value.ToString();
            startDate.Replace('/', '-');
            DateTime date = DateTime.Parse(startDate);
            int periodID = miscCharge.getPeriodID(date);
            int employeeCode = empControl.getEmployeeCode(selectedFname, selectedLname);
            pay = new PayController().getPreviousReports(employeeCode, periodID);
            pay.firstName = selectedFname;
            pay.lastName = selectedLname;
            nameLabel.Text = selectedFname + " " + selectedLname;
            salaryLabel.Text = Math.Round(pay.salary,2) + "";
            tipLabel.Text = Math.Round(pay.tip,2) + "";
            chargesLabel.Text = Math.Round(pay.charges,2) + "";
            lateLabel.Text = Math.Round(pay.late,2) + "";
            absentLabel.Text = Math.Round(pay.absences,2) + "";
            debtsLabel.Text = Math.Round(pay.debt,2) + "";
            bonusesLabel.Text = Math.Round(pay.bonuses,2) + "";
            deductionsLabel.Text = Math.Round(pay.deductions,2) + "";
            sssLabel.Text = Math.Round(pay.SSS,2) + "";
            philHealthLabel.Text = Math.Round(pay.PhilHealth,2) + "";
            payLabel.Text = Math.Round(pay.TotalPay,2) + "";
        }

        private void generateReportButton_Click(object sender, EventArgs e)
        {
            if (viewReportsEmployee.SelectedIndex != -1)
            {
                FolderBrowserDialog reportDirectory = new FolderBrowserDialog();
                reportDirectory.ShowDialog();
                String path = reportDirectory.SelectedPath;
                if (!path.Equals(""))
                {
                    bool success = print(reportDirectory.SelectedPath);
                    if (success)
                    {
                        MessageBox.Show("Successfully generated the report.", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("Unable to generate report", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                MessageBox.Show("No selected employee from list. No reports will be generated.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private bool print(String path)
        {
            Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
            MiscChargeController miscCharge = new MiscChargeController();
            DateTime startDate = DateTime.Parse(viewReportsPeriodTable.SelectedRows[0].Cells[0].Value.ToString());
            DateTime endDate = DateTime.Parse(viewReportsPeriodTable.SelectedRows[0].Cells[1].Value.ToString());
            string title = startDate.ToString("yyyy-MM-dd") + "_" + endDate.ToString("yyyy-MM-dd");
            try
            {
                object oMissing = System.Reflection.Missing.Value;
                Document aDoc = WordApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                WordApp.Visible = false;
                aDoc.Activate();
                WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                WordApp.Selection.TypeText(pay.firstName + " " + pay.lastName);
                WordApp.Selection.TypeParagraph();
                WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
                WordApp.Selection.TypeText("\t\t\t--------------------------------------------------------------------------");
                WordApp.Selection.TypeParagraph();
                WordApp.Selection.TypeText("\t\t\t+Salary:\t\t\t\t\t\t" + Math.Round(pay.salary,2));
                WordApp.Selection.TypeParagraph();
                WordApp.Selection.TypeText("\t\t\t+Tip:\t\t\t\t\t\t" + Math.Round(pay.tip,2));
                WordApp.Selection.TypeParagraph();
                WordApp.Selection.TypeText("\t\t\t-Charges:\t\t\t\t\t" + Math.Round(pay.charges,2));
                WordApp.Selection.TypeParagraph();
                WordApp.Selection.TypeText("\t\t\t-Absent:\t\t\t\t\t" + Math.Round(pay.absences,2));
                WordApp.Selection.TypeParagraph();
                WordApp.Selection.TypeText("\t\t\t-Late:\t\t\t\t\t\t" + Math.Round(pay.late,2));
                WordApp.Selection.TypeParagraph();
                WordApp.Selection.TypeText("\t\t\t-Debts:\t\t\t\t\t\t" + Math.Round(pay.debt,2));
                WordApp.Selection.TypeParagraph();
                WordApp.Selection.TypeText("\t\t\t+Bonuses:\t\t\t\t\t" + Math.Round(pay.bonuses,2));
                WordApp.Selection.TypeParagraph();
                WordApp.Selection.TypeText("\t\t\t-Deductions:\t\t\t\t\t" + Math.Round(pay.deductions,2));
                WordApp.Selection.TypeParagraph();
                WordApp.Selection.TypeText("\t\t\t-SSS:\t\t\t\t\t\t" + Math.Round(pay.SSS,2));
                WordApp.Selection.TypeParagraph();
                WordApp.Selection.TypeText("\t\t\t-PhilHealth:\t\t\t\t\t" + Math.Round(pay.PhilHealth,2));
                WordApp.Selection.TypeParagraph();
                WordApp.Selection.TypeText("\t\t\t--------------------------------------------------------------------------");
                WordApp.Selection.TypeParagraph();
                WordApp.Selection.Font.Bold = (int)WdConstants.wdToggle;
                WordApp.Selection.TypeText("\t\t\tTotal Pay:\t\t\t\t\t" + Math.Round(pay.TotalPay,2));
                WordApp.Selection.TypeParagraph();
                WordApp.ChangeFileOpenDirectory(path);
                object saveFilename = pay.lastName + "_" + pay.firstName + title + ".docx";
                aDoc.SaveAs(ref saveFilename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                aDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
                WordApp = null;
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Export error: " + ex.Message);
                return false;
            }
        }


    }
}
